package org.myabstract.graph.model;

import java.util.ArrayList;
import java.util.List;

public class Graph<T extends INodeType> {

	public static Graph instance = new Graph();

	private List<Node<INodeType>> nodeList = new ArrayList<Node<INodeType>>();
	private Class<T> typeClass = null;

	public Graph() {

		this.nodeList = new ArrayList<Node<INodeType>>();
	}
 
	public <T extends INodeType> List<Node<T>> getChildren(Class<T> typeClass) {
		List<Node<T>> result = new ArrayList<Node<T>>();
		if (typeClass != null) {
			for (Node<INodeType> node : nodeList) {
				if (typeClass.isInstance(node.getType())) {
					((Node<T>) node).isTopLevelElement = true;
					result.add((Node<T>) node);
				}
			}
		}
		return result;
	}

	public boolean addNode(Node<INodeType> nod) {
		if (nod != null)
			if (!this.nodeList.contains(nod))
				if (this.nodeList.add(nod))
					return true;
		return false;
	}

	public boolean removeNode(Node<INodeType> nod) {
		if (this.nodeList.contains(nod))
			if (this.nodeList.remove(nod))
				return true;
		return false;
	}

	// this method is for the ContentProvider
	public Object[] getChildren() {
		// TODO Auto-generated method stub
		if (this.typeClass == null)
			return null;
		List<?> list = this.getChildren(this.typeClass);
		Node<INodeType>[] result = new Node[list.size()];
		for (int i = 0; i < list.size(); i++) {
			result[i] = (Node<INodeType>) list.get(i);
		}
		return result;
	}

	public void setTypeClass(Class<T> tc) {
		this.typeClass = tc;
	}
	
	public void resetGraph(){
		this.nodeList=new ArrayList<Node<INodeType>>();
	}
	
	public List<Node<INodeType>> getNodeList() {
		return this.nodeList;
	}

	
	
}
